package model.trader;

import java.util.ArrayList;
import java.util.List;

import model.market.MarketInformation;
import model.market.MarketManager;
import model.market.Position;
import model.market.Trade;

public class IndexTracker implements Trader{

	boolean equalWeightings=false;
	
	@Override
	public List<Trade> trade(MarketManager market, Position position, MarketInformation marketInfo) {

		//AM: could just trade once but they need to keep reinvesting dividends etc.
		double worth = position.getWorth(marketInfo.getMarketPrices());

		
		
		List<Trade> ret = new ArrayList<Trade>();

		double currentIndexPrice = marketInfo.getIndexPrices().get(marketInfo.getIndexPrices().size()-1);
		
		for(int i=0;i<marketInfo.getNumberOfAssets();i++){
			double currentHolding=position.getExecutedQuantity(i)*marketInfo.getMarketPrices()[i];

			//Weighting based on price because they all start out even..
			double tradeValue = (worth*.99*(marketInfo.getMarketPrices()[i])/currentIndexPrice)-currentHolding;
			if(equalWeightings){
				tradeValue = (worth/marketInfo.getNumberOfAssets())-currentHolding;
			}

			int tradeQuantity = (int)(tradeValue/marketInfo.getMarketPrices()[i]);

			Trade trade = new Trade(this,i,tradeQuantity,(tradeQuantity>0?marketInfo.getMarketPrices()[i]*1.15:marketInfo.getMarketPrices()[i]/1.15));
			ret.add(trade);

		}

		return ret;
	}



}
